<?php
/**
 * @author tungck
 * @filesource department.php
 * quan ly nghanh hoc
*/

class Department extends CI_Controller{
    
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        
        $userSession = $this->session->userdata('user');
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }else{
            if($userSession['useradmin'] != 1){
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
    }
    
    function listDepartment(){
        $title = "Danh sách ngành";
        $this->adminlayout->view("admin/listDepartment",null ,$title);        
    }
    
    function getListDepartment(){
        try{
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/department_model");
        $result = $this->department_model->getList($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
		$jTableResult['Records'] = $result['query'];
		print json_encode($jTableResult);
        }catch(Exception $ex){
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Records'] = $ex->message();
            print json_encode($jTableResult);
        }
    }
    
    
    function addDepartment(){
        $jTableResult = array();
        if(!empty($_POST['TenNganh'])){
            $data['TenNganh'] = $_POST['TenNganh'];
            $this->load->model("admin/department_model");
            $result = $this->department_model->insertDepartment($data);
            $jTableResult['Result'] = "OK";
            $jTableResult['Record'] = $result;
            print json_encode($jTableResult);
        }else{
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Message'] = array('Nhập Tên Ngành');
   	        print json_encode($jTableResult);
        }
    }
    
    function deleteDepartment(){
        try{
        if(isset($_POST["MaNganh"])){
            $id = $_POST["MaNganh"];
            $this->load->model("admin/department_model");
            $this->department_model->deleteDepartment($id);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
            
        }
        }catch(Exception $ex){
            $jTableResult = array();
	        $jTableResult['Result'] = "ERROR";
	           $jTableResult['Message'] = $ex->message();
    	       print json_encode($jTableResult);
        }
        
    }
    
    function updateDepartment(){
        $jTableResult = array();
        if(!empty($_POST['TenNganh'])){
            $data['MaNganh'] = $_POST['MaNganh'];
            $data['TenNganh'] = $_POST['TenNganh'];
            $this->load->model("admin/department_model");
            $this->department_model->updateDepartment($data);
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
        }else{
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Message'] = array('Nhập Tên Ngành');
   	        print json_encode($jTableResult);
        }
    }
    
    
}

?>